import requests
import re
import time


class All_xs(object):
    def __init__(self):
        self.url = 'https://www.17k.com/list/3511219.html'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
        }

    def resp_get(self):
        response = requests.get(self.url, headers=self.headers)
        response.encoding='utf-8'
        if response.status_code == 200:
            return response.text
        else:
            return None

    def var_get(self, response):
        r1 = re.findall(r'''<a target="_blank"
					   href="(.*?)"
					   title="(.*?)">
							<span class="ellipsis ">
									(.*?)
							</span>
					</a>''', response)

        for data in r1:
            a,n,b=data
            b2=b.strip()
            var1='https://www.17k.com'+a
            self.My_get(var1,b2)
            time.sleep(1)

    def My_get(self, var1,b2):
        response = requests.get(var1, headers=self.headers)
        response.encoding='utf-8'
        r1 = re.findall(r'<p>(.*)</p>', response.text)
        for data in r1:
            with open(f'./01--小说/{b2}.txt', 'w', encoding='utf-8') as f:
                f.write(data)
        print(f'{b2}---保存成功')

    def main(self):
        self.var_get(self.resp_get())


if __name__ == '__main__':
    All = All_xs()
    All.main()
